package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"
)


//咨询表
type Consult struct {
	Id               int64
	Protype          string
	Consulttime      string
	Consultcontent   string
	Replycontent     string
	Status           string
	Iseffect         string

}
//建立模型
func init() {
	orm.RegisterModel(new(Consult))
}

//获取咨询列表
func GetConsultlist(page int64, page_size int64, sort string) (consult []orm.Params, count int64) {
	o := orm.NewOrm()
	Consult := new(Consult)
	qs := o.QueryTable(Consult).Filter("Iseffect", "0")
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * page_size
	}
	qs.Limit(page_size, offset).OrderBy(sort).Values(&consult)
	count, _ = qs.Count()
	fmt.Println("consult:", consult)
	fmt.Println("count:", count)
	return consult, count
}

func QueryConsult(Consult_id int64) (consult []orm.Params) {
	o := orm.NewOrm()
	Consult := new(Consult)
	qs := o.QueryTable(Consult).Filter("id", Consult_id)
	qs.Values(&consult)
	return consult
}

func UpdateConsult(Replycontent string,Consult_id int64) (int64, error) {
	o := orm.NewOrm()
	new := new(Consult)
	//更改之前必须先指定主键
	new.Id = Consult_id
	//要更新的内容
	new.Status = "已回复"
	new.Replycontent=Replycontent
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(new, "Status","Replycontent")
	return num, err

}
